﻿using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.SharePoint.Administration;

namespace ITSP.SharePoint.CacheRefresh.Examples.Configuration
{
    /// <summary>
    /// An Example Configuration Manager to load and save the Example Configuration.
    /// </summary>
    public class ExampleConfigurationManager : BaseSPConfigurationManager
    {
        #region Singleton Pattern

        private static ExampleConfigurationManager _configurationManagerSingleton = null;
        private static object syncRoot=new object();

        /// <summary>
        /// Singleton pattern to the get the configuration
        /// </summary>
        /// <returns>Gets the Current Site Creator Configuration Manager</returns>
        public static ExampleConfigurationManager Current
        {
            get
            {
                try
                {
                    if (_configurationManagerSingleton == null)
                    {
                        lock (syncRoot)
                        {
                            _configurationManagerSingleton = new ExampleConfigurationManager();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Trace.TraceError("ExampleConfigurationManager.GetCurrent() {0}", ex.Message);
                    throw ex;
                }

                return _configurationManagerSingleton;
            }



        }

        #endregion

        /// <summary>
        /// Default Constructor.
        /// This loads the configuration.
        /// </summary>
        public ExampleConfigurationManager()
            :base(GetWebApplication().Sites.First().Url)
        {
            base._configurationStorageKey = "PROP_EXAMPLE_CONFIGURATION";
            _cacheKey = "Example";
            _listCacheKey = _cacheKey;
            ExampleConfiguration configuration = Configuration;
        }

        /// <summary>
        /// Constructor.
        /// This loads the configuration manager and passes in url where the configuration cache is hosted.
        /// </summary>
        public ExampleConfigurationManager(string hostUrl)
            : base(hostUrl)
        {
            base._configurationStorageKey = "PROP_EXAMPLE_CONFIGURATION";
            _cacheKey = "Example";
            _listCacheKey = _cacheKey;
            ExampleConfiguration configuration = Configuration;
        }

        /// <summary>
        /// Gets the Example Configuration
        /// </summary>
        public ExampleConfiguration Configuration
        {
            get
            {
                ExampleConfiguration configuration = Load<ExampleConfiguration>();
                return configuration;
            }
            set
            {
                ExampleConfiguration configuration = value as ExampleConfiguration;
                if (configuration != null)
                {
                    configuration.Modified = DateTime.Now;
                    Save<ExampleConfiguration>(configuration);
                }
                else
                {
                    throw new InvalidOperationException("Configuration object not of Type ExampleConfiguration");
                }
            }
        }

        private static SPWebApplication GetWebApplication()
        {
            SPWebService webService = SPFarm.Local.GetChild<SPWebService>();
            SPWebApplication webApplication = webService.WebApplications.FirstOrDefault(wa => wa.IsAdministrationWebApplication == false);
            return webApplication;
        }
    }
}
